home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-01-28 | 11.7 KB | 523 lines | [TEXT/MPS ] |
- /*
- File: ScriptUtils.cp
-
- Contains: Script handling & OSA interface
-
-
- Copyright: Apple Computer Inc, all rights reserved.
-
- The routines in this file are drawn from previous DTS exampleware.
-
- */
-
-
- #include "ScriptUtils.h"
- #include "SimpliFaceCommon.h"
-
- #include <StdArg.h>
- #include <Sysequ.h>
-
- #ifndef __LIMITS__
- #include <Limits.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
-
- #ifndef __PROCESSES__
- #include <Processes.h>
- #endif
-
- #ifndef __PLSTRINGFUNCS__
- #include <PLStringFuncs.h>
- #endif
-
- #ifndef __AEOBJECTS__
- #include <AEObjects.h>
- #endif
-
-
- #pragma trace off
-
-
-
- pascal OSErr GetCStringFromDescriptor(const AEDesc *sourceDesc, CStr255 *resultStr)
- {
- OSErr err;
- Size stringSize = 0;
- AEDesc resultDesc;
- char buff[260];
-
- resultDesc.dataHandle = nil;
-
- *resultStr = "";
-
- err = AECoerceDesc(sourceDesc, typeChar, &resultDesc);
-
- if (!err)
- GetRawDataFromDescriptor(&resultDesc, (Ptr)buff,
- 255, &stringSize);
- if (stringSize<256)
- {
- buff[stringSize] = 0;
- *resultStr = buff;
- }
- else
- err = errAECoercionFail;
-
- if (resultDesc.dataHandle)
- AEDisposeDesc(&resultDesc);
-
- return err;
- }
-
-
-
- OSErr MakeNullDesc(AEDesc *dataDesc)
- {
- return AECreateDesc(typeNull, NULL, 0, dataDesc);
- }
-
-
- OSErr MakeNameDesc(CStr255& theString, AEDesc *dataDesc)
- {
- return AECreateDesc(typeChar, (char*)theString,
- theString.Length(), dataDesc);
- }
-
-
- OSErr MakeLongIntDesc(long theIndex, AEDesc *dataDesc)
- {
- return AECreateDesc(typeLongInteger, &theIndex, sizeof(long), dataDesc);
- }
-
-
-
- //----------------------------------------------------------------------------------//
- // Initializes all descriptor records passed in to this routine. The variable //
- // argument list is null terminated. //
- //----------------------------------------------------------------------------------//
- #pragma segment Utils
- void InitAEDescs(AEDesc* desc1, ... ) // Variable, null terminated argument list.
- {
- va_list argptr; // pointer to each argument in list.
- AEDesc* nextDesc; // next descriptor argument in list.
-
- va_start(argptr, desc1);
- desc1->descriptorType = typeNull;
- desc1->dataHandle = nil;
-
- while((nextDesc = va_arg(argptr, AEDesc *)))
- {
- nextDesc->descriptorType = typeNull;
- nextDesc->dataHandle = nil;
- }
- va_end(argptr);
- }
-
- //----------------------------------------------------------------------------------//
- // Dispose all descriptor records passed into this routine. (Variable arg. list). //
- //----------------------------------------------------------------------------------//
- #pragma segment Utils
- void DisposeAEDescs(AEDesc* desc1, ... ) // Null terminated argument list.
- {
- va_list argptr; // pointer to each argument in list.
- AEDesc* nextDesc; // descriptor argument in list.
-
- va_start(argptr, desc1);
- if (desc1->dataHandle)
- AEDisposeDesc(desc1);
-
- while((nextDesc = va_arg(argptr, AEDesc *)))
- {
- if (nextDesc->dataHandle)
- AEDisposeDesc(nextDesc);
- }
- va_end(argptr);
- }
-
- /**-----------------------------------------------------------------------
- Name: LesserOf
- Purpose: Returns the Lesser of two longints.
- -----------------------------------------------------------------------**/
- #pragma segment Utils
-
- pascal long LesserOf(long A, long B)
- {
- if (A<B)
- return(A);
- else
- return(B);
- } /*LesserOf*/
-
- /**-----------------------------------------------------------------------
- Name: GreaterOf
- Purpose: Returns the Greater of two longints.
- -----------------------------------------------------------------------**/
-
- #pragma segment Utils
-
- pascal long GreaterOf(long A, long B)
- {
- if (A>B)
- return(A);
- else
- return(B);
- } /*GreaterOf*/
-
-
- /**-----------------------------------------------------------------------
- Utility Routines for getting data from AEDesc's
- -----------------------------------------------------------------------**/
-
- pascal void GetRawDataFromDescriptor(const AEDesc *theDesc,
- Ptr destPtr,
- Size destMaxSize,
- Size *actSize)
- {
- Size copySize;
-
-
- if (theDesc->dataHandle)
- {
- HLock((Handle)theDesc->dataHandle);
- *actSize = GetHandleSize((Handle)theDesc->dataHandle);
-
- copySize = LesserOf(*actSize, destMaxSize);
-
- BlockMove(*theDesc->dataHandle, destPtr, copySize);
-
- HUnlock((Handle)theDesc->dataHandle);
- }
- else
- *actSize = 0;
- } /*GetRawDataFromDescriptor*/
-
- pascal OSErr GetPStringFromDescriptor(const AEDesc *sourceDesc, char *resultStr)
-
- {
- OSErr myErr;
- OSErr ignoreErr;
- Size stringSize;
- AEDesc resultDesc;
-
- resultDesc.dataHandle = nil;
-
- resultStr[0] = 0;
-
- myErr = AECoerceDesc(sourceDesc,typeChar,&resultDesc);
-
- if (myErr==noErr)
- GetRawDataFromDescriptor(&resultDesc,
- (Ptr)&resultStr[1],
- 250,
- &stringSize);
- if (stringSize<250)
- resultStr[0] = (char)stringSize;
- else
- resultStr[0] = (char)250;
-
- if (resultDesc.dataHandle)
- ignoreErr = AEDisposeDesc(&resultDesc);
-
- return(myErr);
- }
-
- pascal OSErr GetIntegerFromDescriptor(const AEDesc *sourceDesc, short *result)
- {
- OSErr myErr;
- OSErr ignoreErr;
- Size intSize;
- AEDesc resultDesc;
-
- *result = 0;
- myErr = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc);
-
- if (myErr==noErr)
- {
- GetRawDataFromDescriptor(&resultDesc,
- (Ptr)result,
- 2,
- &intSize);
- if (intSize>2)
- myErr = errAECoercionFail;
- }
-
- if (resultDesc.dataHandle)
- ignoreErr = AEDisposeDesc(&resultDesc);
-
- return(myErr);
- }
-
-
- pascal OSErr GetDescTypeFromDescriptor(const AEDesc *sourceDesc, DescType *result)
- {
- OSErr myErr;
- OSErr ignoreErr;
- Size descSize;
- AEDesc resultDesc;
-
- *result = typeNull;
- myErr = AECoerceDesc(sourceDesc,typeShortInteger,&resultDesc);
-
- if (myErr==noErr)
- {
- GetRawDataFromDescriptor(&resultDesc,
- (Ptr)result,
- sizeof(DescType),
- &descSize);
- if (descSize != sizeof(DescType))
- myErr = errAECoercionFail;
- }
-
- if (resultDesc.dataHandle)
- ignoreErr = AEDisposeDesc(&resultDesc);
-
- return(myErr);
- }
-
- pascal OSErr GetBooleanFromDescriptor(const AEDesc *sourceDesc,
- Boolean *result)
- {
- OSErr myErr;
- OSErr ignoreErr;
- Size boolSize;
- AEDesc resultDesc;
-
- *result = false;
- myErr = AECoerceDesc(sourceDesc,typeBoolean,&resultDesc);
-
- if (myErr==noErr)
- {
- GetRawDataFromDescriptor(&resultDesc,
- (Ptr)result,
- sizeof(Boolean),
- &boolSize);
- if (boolSize>sizeof(Boolean))
- myErr = errAECoercionFail;
- }
-
- if (resultDesc.dataHandle)
- ignoreErr = AEDisposeDesc(&resultDesc);
-
- return(myErr);
- }
-
- pascal OSErr GetLongIntFromDescriptor(const AEDesc *sourceDesc,
- long *result)
- {
- OSErr myErr;
- OSErr ignoreErr;
- Size intSize;
- AEDesc resultDesc;
-
- *result = 0;
- myErr = AECoerceDesc(sourceDesc,typeLongInteger,&resultDesc);
-
- if (myErr==noErr)
- {
- GetRawDataFromDescriptor(&resultDesc,
- (Ptr)result,
- 4,
- &intSize);
- if (intSize>4)
- myErr = errAECoercionFail;
- }
-
- if (resultDesc.dataHandle)
- ignoreErr = AEDisposeDesc(&resultDesc);
-
- return(myErr);
- } /*GetLongIntFromDescriptor*/
-
- pascal OSErr GetRectFromDescriptor(const AEDesc *sourceDesc, Rect *result)
- {
- OSErr myErr;
- OSErr ignoreErr;
- Size rectSize;
- AEDesc resultDesc;
-
- SetRect(result,0,0,0,0);
- myErr = AECoerceDesc(sourceDesc,typeQDRectangle,&resultDesc);
-
- if (myErr==noErr)
- {
- GetRawDataFromDescriptor(&resultDesc,
- (Ptr)result,
- sizeof(Rect),
- &rectSize);
- if (rectSize<sizeof(Rect))
- myErr = errAECoercionFail;
- }
-
- if (resultDesc.dataHandle)
- ignoreErr = AEDisposeDesc(&resultDesc);
-
- return(myErr);
- } /*GetRectFromDescriptor*/
-
- pascal OSErr GetPointFromDescriptor(const AEDesc *sourceDesc,
- Point *result)
- {
- OSErr myErr;
- OSErr ignoreErr;
- Size ptSize;
- AEDesc resultDesc;
-
- SetPt(result,0,0);
-
- myErr = AECoerceDesc(sourceDesc,typeQDPoint,&resultDesc);
-
- if (myErr==noErr)
- {
- GetRawDataFromDescriptor(&resultDesc,
- (Ptr)result,
- sizeof(Point),
- &ptSize);
-
- if (ptSize<sizeof(Point))
- myErr = errAECoercionFail;
-
- }
-
- if (resultDesc.dataHandle)
- ignoreErr = AEDisposeDesc(&resultDesc);
-
- return(myErr);
- } /*GetPointFromDescriptor*/
-
-
- /*******************************************************************************/
- /*
- Object Accessors - Utility Routines
- */
-
- #pragma segment ObjectAccessors
-
- pascal WindowPtr WindowNameToWindowPtr(StringPtr nameStr)
- /*
- Returns the WindowPtr of the window with title nameStr
- or nil if there is no matching window.
- */
- {
- WindowPtr theWindow;
- Str255 windTitle;
-
- theWindow = (WindowPtr)*((Handle)WindowList);
- /*
- iterate through windows - we use WindowList 'cos we could
- have made the window invisible and we lose it - so we
- can't set it back to visible!!
- */
- while (theWindow)
- {
- GetWTitle(theWindow, windTitle);
- if (EqualString(windTitle,
- nameStr,
- false,
- true)) /* ignore case, don't ignore diacriticals */
- return(theWindow);
- theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
- }
- return(theWindow);
- } /* WindowNameToWindowPtr */
-
- pascal WindowPtr GetWindowPtrOfNthWindow(short index)
- /* returns a ptr to the window with the given index
- (front window is 1, behind that is 2, etc.). if
- there's no window with that index (inc. no windows
- at all), returns nil.
- */
- {
- WindowPtr theWindow;
-
- theWindow = (WindowPtr)*((Handle)WindowList);
-
- /* iterate through windows */
-
- while (theWindow)
- {
- index --;
- if (index <= 0)
- return(theWindow);
-
- theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
- }
- return(nil);
- } /* GetWindowPtrOfNthWindow */
-
- pascal short CountWindows(void)
- {
- WindowPtr theWindow;
- short index;
-
- index = 0;
- theWindow = (WindowPtr)*((Handle)WindowList);
-
- /* iterate through windows */
-
- while (theWindow)
- {
- index++;
- theWindow = (WindowPtr)((WindowPeek)theWindow)->nextWindow;
- }
-
- return(index);
-
- } /*CountWindows*/
-
-
-
- /* Next bit of code nabbed from SignatureToApp */
-
- pascal OSErr FindApplicationOnVolume(OSType whatSig, short theVol, FSSpec *foundSpec)
- {
- OSErr myErr;
- DTPBRec myPB;
- HParamBlockRec myHParams;
- GetVolParmsInfoBuffer theVolInfo;
-
- myHParams.ioParam.ioCompletion = nil;
- myHParams.ioParam.ioNamePtr = nil;
- myHParams.ioParam.ioVRefNum = theVol;
- myHParams.ioParam.ioBuffer = (Ptr)&theVolInfo;
- myHParams.ioParam.ioReqCount = sizeof(theVolInfo);
-
- myErr = PBHGetVolParms(&myHParams, false); /* synchchronous */
-
- if (myErr==noErr && (theVolInfo.vMAttrib & (1 << bHasDesktopMgr)) != 0)
- { /* Have a deskTop database */
- myPB.ioCompletion = nil;
- myPB.ioVRefNum = theVol;
- myPB.ioNamePtr = nil;
-
- myErr = PBDTGetPath(&myPB); /* Gets myPB.ioDTRefNum */
-
- if (myErr==noErr)
- {
- myPB.ioCompletion = nil;
- myPB.ioIndex = 0;
- myPB.ioFileCreator= whatSig;
- myPB.ioNamePtr = (StringPtr)&foundSpec->name[0];
-
- myErr = PBDTGetAPPL(&myPB, false);
-
- if (myErr == noErr)
- {
- foundSpec->vRefNum = theVol;
- foundSpec->parID = myPB.ioAPPLParID;
- }
- }
- }
- return(myErr);
- }
-